package nz.co.doherty.gwtpbreadcrumbs.client.ui.customers;

import nz.co.doherty.gwtpbreadcrumbs.client.NameTokens;
import nz.co.doherty.gwtpbreadcrumbs.client.gin.ClientGinjector;
import nz.co.doherty.gwtpbreadcrumbs.client.i8n.ClientConstants;
import nz.co.doherty.gwtpbreadcrumbs.client.ui.app.AppPresenter;
import nz.co.doherty.gwtpbreadcrumbs.shared.entity.Customer;
import nz.co.doherty.gwtpbreadcrumbs.shared.rpc.customer.CustomerCreateAction;
import nz.co.doherty.gwtpbreadcrumbs.shared.rpc.customer.CustomerCreateResult;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.philbeaudoin.gwtp.dispatch.client.DispatchAsync;
import com.philbeaudoin.gwtp.mvp.client.EventBus;
import com.philbeaudoin.gwtp.mvp.client.PresenterImpl;
import com.philbeaudoin.gwtp.mvp.client.annotations.NameToken;
import com.philbeaudoin.gwtp.mvp.client.annotations.ProxyCodeSplit;
import com.philbeaudoin.gwtp.mvp.client.annotations.TitleFunction;
import com.philbeaudoin.gwtp.mvp.client.proxy.PlaceManager;
import com.philbeaudoin.gwtp.mvp.client.proxy.ProxyPlace;
import com.philbeaudoin.gwtp.mvp.client.proxy.RevealContentEvent;

public class CustomerCreatePresenter
		extends
		PresenterImpl<CustomerCreateView, CustomerCreatePresenter.CustomerCreateProxy>
		implements CustomerCreateView.Presenter {

	@ProxyCodeSplit
	@NameToken(NameTokens.customerCreate)
	public interface CustomerCreateProxy extends
			ProxyPlace<CustomerCreatePresenter> {
	}

	private final DispatchAsync dispatcher;
	private final PlaceManager placeManager;
	private final ClientConstants constants;

	@Inject
	public CustomerCreatePresenter(EventBus eventBus, CustomerCreateView view,
			CustomerCreateProxy proxy, PlaceManager placeManager,
			ClientConstants constants, DispatchAsync dispatcher) {
		super(eventBus, view, proxy);
		this.placeManager = placeManager;
		this.constants = constants;
		this.dispatcher = dispatcher;
	}

	@Override
	protected void onBind() {
		super.onBind();
	}

	@Override
	protected void onReset() {
		super.onReset();
		this.view.reset();
	}

	@Override
	protected void onReveal() {
		super.onReveal();
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(eventBus, AppPresenter.TYPE_RevealMainContent,
			this);
	}

	@TitleFunction
	public String getPageTitle(ClientGinjector ginjector) {
		return ginjector.getConstants().createCustomer();
	}

	@Override
	public void onCreate() {
		Customer customer = new Customer(this.view.getFirstName(), this.view.getLastName());
		this.dispatcher.execute(new CustomerCreateAction(customer), new AsyncCallback<CustomerCreateResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(CustomerCreateResult result) {
				// TODO Auto-generated method stub
				placeManager.revealRelativePlace(-1);				
			}		
		});		
	}
}
